<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/25 0025
 * Time: 9:19
 */
namespace app\api\controller;

use app\api\model\Api;
use app\api\model\JWT;
use think\App;
use think\Controller;
use think\Db;
use app\api\model\Role as RoleModel;

/**
 * @title 数据
 * @description 接口说明
 * @header name:flag require:1 default: desc:手机设备号
 */
class Material extends Api
{
    /**
     * @title 宴会数据
     * @description 接口说明
     * @author 开发者
     * @url /api/Material/index
     * @method POST
     * @param name:year type:string require:1 default: other: desc:年
     * @param name:month type:string require:1 default: other: desc:月
     * @param name:manager_id type:int require:1 default: other: desc:客户经理
     * 
     * 
     * @return scheduled:预定订单
     * @return check:结单订单
     * @return submersible:潜客
     * @return deposit:押金
     * @return check_money:结单
     * @return retired:退单
     * @return banquet_type:宴会厅类型@
     * @banquet_type  name:名称 type_count:数量
     * 
     * @return banquet:宴会厅使用@
     *  @banquet name:名称 banquet_count:数量
     * 
     * 
     * @return sources:客户来源@
     *  @sources  id:id  name:名称 source_count:获客数  source_counts:成交数
     * 
     * @return trend:成交趋势图@
     * @trend month:月 num:订单数量 counum:结单数
     * 
     */
   public function index()
   {
        $user = $this->user;
        if(!input('year') || !input('month') || !input('manager_id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $year = input('year');
        $month = input('month');
        $manager_id = input('manager_id');


        if($manager_id != $user['id']){

            $pid = db::name('admin_role')->where('id','in',$user['role'])->field('pid')->select();
            $pids = 0;
            foreach ($pid as $vs) {
                if($vs['pid'] == 0){
                    $pids = 1;
                    break;
                }else{
                    $pids = 2;
                }
            }
            if($pids == 2){
                $res = RoleModel::roleAuth($user['role']);
                $newArrs = [];
                if($res){
                    foreach ($res as $k => $v) {
                        $str1 =str_replace("[","",$res[$k]);
                        $str1 =str_replace("]","",$str1);
                        $newArr = explode(',',$str1);
                        $newArrs = array_merge($newArr,$newArrs);
                    }
                    foreach ($newArrs as $kn => $vn) {
                        $vn =str_replace('"',"",$vn);
                        $newArrs[$kn] = $vn;
                    }
                    if(!in_array('329',$newArrs)){
                        return $this->ajaxReturn(1,'暂无查看权限');
                    }
                }
            }
    
        }

        $where[] = ['begin_year','=',$year];
        $where[] = ['begin_month','=',$month];
        $where[] = ['director_id','=',$manager_id];
        $where[] = ['hid','=',$user['hid']];
        // 预定
        $scheduled = db::name('scheduled')->where('type',2)->where($where)->count();
        // 结单
        $check = db::name('scheduled')->where('type',3)->where($where)->count();
        // 潜客
        $submersible = db::name('scheduled')->where('type',1)->where($where)->count();
        // 押金
        $times = $year. '-' .$month;
        $time = strtotime($times);
        $end = strtotime("+1months",$time);
        $deposit = db::name('scheduled_deposit')
        ->where('addtime','between',[$time,$end])
        ->where('hid',$user['hid'])
        ->where('manager_id',$manager_id)
        ->where('status',3)
        ->sum('money');
        // // 结单金额
        $check_money = db::name('check_order')
        ->where('addtime','between',[$time,$end])
        ->where('hid',$user['hid'])
        ->where('manager_id',$manager_id)
        ->sum('total_money');
        // // 退订订单
        $retired = db::name('scheduled')->where('is_del',1)->where($where)->count();
        // // 趋势图
        $yeara = date("Y-m-d H:i:s",time());
        $yeara = explode("-",$yeara);
        $months = $yeara[1];
        $years = $yeara[0];
        $newArr = [];
        for ($i=1; $i <=$months; $i++) { 
            $a = $i-1;
            $newArr[$a]['month'] = $i;
            $newArr[$a]['num'] = db::name('scheduled')
            ->where('begin_year',$years)
            ->where('begin_month',$i)
            ->where('type',3)
            ->where('director_id',$manager_id)
            ->where('hid',$user['hid'])->count();

            $times = $year. '-' .$i;
            $time = strtotime($times);
            $end = strtotime("+1months",$time);
            $newArr[$a]['counum'] = db::name('check_order')
            ->where('addtime','between',[$time,$end])
            ->where('hid',$user['hid'])
            ->count();
        }
        // print_r($newArr);die;
        // 宴会厅类型
        $banquet_type = db::name('banquet_types')->where('is_del',1)->where('hid',$user['hid'])->field('id,name')->select();
        if($banquet_type){
            foreach ($banquet_type as $ks => $vs) {
                $count = db::name('scheduled')->where('tid',$vs['id'])->where($where)->count();
                $banquet_type[$ks]['type_count'] = $count;
            }
        }
        // 宴会厅使用
        $banquet = db::name('banquet')->where('hid',$user['hid'])->where('is_del',1)->field('id,name')->select();
        if($banquet){
            foreach ($banquet as $kv => $vv) {
                $count = db::name('scheduled')->where('bid',$vv['id'])->where($where)->count();
                $banquet[$kv]['banquet_count'] = $count;
            }
        }
        // 客户来源
        $source = db::name('source')->where('is_del',1)->where('hid',$user['hid'])->where('type',1)->field('id,name')->select();
        if($source){
            foreach ($source as $kv => $vv) {
                $count = db::name('scheduled')->where('source',$vv['id'])->where($where)->count();
                $counts = db::name('scheduled')->where('source',$vv['id'])->where('type',3)->where($where)->count();
                $source[$kv]['source_count'] = $count;
                $source[$kv]['source_counts'] = $counts;
            }
        }

        // 宴会厅使用
        $banqueta = array_column($banquet, 'banquet_count');
        array_multisort($banqueta, SORT_DESC, $banquet);


        // 客户来源
        $sources = array_column($source, 'source_count');
        array_multisort($sources, SORT_DESC, $source);


        // 宴会类型
        $banquet_types = array_column($banquet_type, 'type_count');
        array_multisort($banquet_types, SORT_DESC, $banquet_type);

        // 预定订单
        $data['scheduled'] = $scheduled;
        // 结单订单
        $data['check'] = $check;
        // 潜客
        $data['submersible'] = $submersible;
        // 押金
        $data['deposit'] = $deposit;
        // 结单
        $data['check_money'] = $check_money;
        // 退单
        $data['retired'] = $retired;
        // 宴会厅类型
        $data['banquet_type'] = $banquet_type;
        // 宴会厅使用
        $data['banquet'] = $banquet;
        // 客户来源
        $data['sources'] = $source;
        // 成交趋势图
        $data['trend'] = $newArr;
        return $this->ajaxReturn(0,'成功',$data);
   }
   /**
     * @title 零点数据
     * @description 接口说明
     * @author 开发者
     * @url /api/Material/zero
     * @method POST
     * @param name:year type:string require:1 default: other: desc:年
     * @param name:month type:string require:1 default: other: desc:月
     * @param name:manager_id type:int require:1 default: other: desc:客户经理
     * 
     * 
     * @return scheduled:预定订单
     * @return effective:有效预定
     * @return overturn:翻台次数
     * @return sources:客户来源@
     * 
     * @sources id name:名称 source_count:获客数  source_counts:成交数
     * 
     * @return trend:成交趋势图@
     * @trend month:月 num:订单数量
     * 
     */
   public function zero()
   {
    $user = $this->user;
    if(!input('year') || !input('month') || !input('manager_id')){
        return $this->ajaxReturn(1,'缺少参数');
    }
    $year = input('year');
    $month = input('month');
    $manager_id = input('manager_id');





    $where[] = ['year','=',$year];
    $where[] = ['month','=',$month];
    $where[] = ['director_id','=',$manager_id];
    $where[] = ['hid','=',$user['hid']];
        // 预定
        $scheduled = db::name('zero')->where($where)->count();
        // 有效预定
        $map['status'] = array('in','4,2');
        $effective = db::name('zero')->where($where)->where($map)->count();
        // 翻台
        $overturn = db::name('zero')->where($where)->where('status',4)->count();
        // 客户来源
        $source = db::name('source')->where('hid',$user['hid'])->where('type',2)->field('id,name')->select();
        if($source){
            foreach ($source as $kv => $vv) {
                $count = db::name('zero')->where('source_id',$vv['id'])->where($where)->count();
                $counts = db::name('zero')->where('source_id',$vv['id'])->where('status',4)->where($where)->count();
                $source[$kv]['source_count'] = $count;
                $source[$kv]['source_counts'] = $counts;
            }
        }
         // // 趋势图
         $yeara = date("Y-m-d H:i:s",time());
         $yeara = explode("-",$yeara);
         $months = $yeara[1];
         $years = $yeara[0];
         for ($i=1; $i <=$months ; $i++) { 
            $a = $i-1;
            $newArr[$a]['month'] = $i;
            $newArr[$a]['num'] = db::name('zero')
             ->where('year',$years)
             ->where('month',$i)
             ->where('status',4)
             ->where('director_id',$manager_id)
             ->where('hid',$user['hid'])->count();
         }


         
        // 客户来源
        $sources = array_column($source, 'source_count');
        array_multisort($sources, SORT_DESC, $source);


        if($manager_id != $user['id']){

            $pid = db::name('admin_role')->where('id','in',$user['role'])->field('pid')->select();
            $pids = 0;
            foreach ($pid as $vs) {
                if($vs['pid'] == 0){
                    $pids = 1;
                    break;
                }else{
                    $pids = 2;
                }
            }
            if($pids == 2){
                $res = RoleModel::roleAuth($user['role']);
                $newArrs = [];
                if($res){
                    foreach ($res as $k => $v) {
                        $str1 =str_replace("[","",$res[$k]);
                        $str1 =str_replace("]","",$str1);
                        $newArr = explode(',',$str1);
                        $newArrs = array_merge($newArr,$newArrs);
                    }
                    foreach ($newArrs as $kn => $vn) {
                        $vn =str_replace('"',"",$vn);
                        $newArrs[$kn] = $vn;
                    }
                    if(!in_array('330',$newArrs)){
                        return $this->ajaxReturn(1,'暂无查看权限');
                    }
                }
            }
    
        }


          // 预定订单
        $data['scheduled'] = $scheduled;
        // 有效预定
        $data['effective'] = $effective;
        // 翻台
        $data['overturn'] = $overturn;
        // 客户来源
        $data['sources'] = $source;
        // 成交趋势图
        $data['trend'] = $newArr;
        return $this->ajaxReturn(0,'成功',$data);

   }
       /**
     * @title 查看客户经理
     * @description 接口说明
     * @author 开发者
     * @url /api/Material/view
     * @method POST
     * 
     */
    public function view()
    {
        $user = $this->user;
        $manager = db::name('user')->where('hid',$user['hid'])->field('id,username')->select();
        $data['list'] = $manager;
        return $this->ajaxReturn(0,'成功',$data);
       
    }
}
